package org.calzz.rpc.exporter;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
 * @author Marek Brodziak <marek.brodziak@gmail.com>
 * @author Piotr Pejas <piotr.pejas@gmail.com>
 * @since Mar 7, 2008
 */
public class RemoteMethodInfo {
	private Method method;
	private Map<String, RemoteParameterInfo> parametersByName = new HashMap<String, RemoteParameterInfo>();
	private Map<Integer, RemoteParameterInfo> parametersByOrdinal = new TreeMap<Integer, RemoteParameterInfo>();
	
	/**
	 * @return
	 */
	public Method getMethod() {
		return method;
	}

	/**
	 * @param method
	 */
	public void setMethod(Method method) {
		this.method = method;
	}

	/**
	 * @param parameterName
	 * @return
	 */
	public Integer getOrdinal(String parameterName) {
		return parametersByName.get(parameterName).getOrdinal();
	}

	/**
	 * @param parameterName
	 * @return
	 */
	public RemoteParameterInfo getRemoteParameterInfo(String parameterName) {
		if (Character.isDigit(parameterName.charAt(0))) {
			return parametersByName.get(Integer.parseInt(parameterName));
		}
		return parametersByName.get(parameterName);
	}

	public RemoteParameterInfo getRemoteParameterInfo(Integer parameterOrdinal) {
		return parametersByOrdinal.get(parameterOrdinal);
	}

	/**
	 * @param parameterName
	 * @param remoteParameterInfo
	 */
	public void putParameter(String parameterName, Integer ordinal, RemoteParameterInfo remoteParameterInfo) {
		parametersByName.put(parameterName, remoteParameterInfo);
		parametersByOrdinal.put(ordinal, remoteParameterInfo);
	}

	/**
	 * @return
	 */
	public int getParametersCount() {
		return parametersByOrdinal.size();
	}
	
	public Collection<RemoteParameterInfo> getParameters() {
		return parametersByOrdinal.values();
	}
}
